<template>
  <div>
    <!-- 按钮 -->

    <el-button style="margin:10px 10px" type="primary" icon="el-icon-refresh-right" circle
      @click="getDynamicExtensionProcessList"></el-button>
    <el-button type="primary" @click="add()">添加</el-button>
    <!--     <el-select v-model="sortList.sortName" style="margin-left: 50px;" @change='sortListChange(sortList.id)'>
        <el-option v-for="v in sortList" :key="v.id" :value="v.id" :label="v.sortName">
        </el-option>
      </el-select> -->
    <el-dropdown @command="handleCommand" style="margin-left: 50px;">
      <span class="el-dropdown-link">
        {{sName || '问题类型'}}<i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item :command="index" v-for="(v,index) in sortList" :key="v.id">{{v.sortName}}</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>

<!--    <el-dropdown @command="handleCommand2" style="margin-left: 50px;">
      <span class="el-dropdown-link">
        {{rName || '下拉菜单'}}<i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item command="0" >热门</el-dropdown-item>
         <el-dropdown-item command="1" >非热门</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown> -->

    <el-table style="width: 100%;" border :data="list">
      <!-- <el-table-column prop="createId" label="名称" align="center">
        </el-table-column> -->
      <!--     <el-table-column prop="memberLevel" label="会员等级" align="center">
                <template slot-scope="{ row, $index }">
                    <div>{{ row.memberLevel == "1" ? 'vip' : 'svip' }}</div>
                </template>
            </el-table-column> -->
      <el-table-column prop="questionContent" label="问题内容" align="center">
      </el-table-column>
      <el-table-column prop="answerContent" label="回答内容" align="center">
      </el-table-column>
      <!--            <el-table-column prop="costMoney" label="消费金额" align="center">
            </el-table-column> -->

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="{ row, $index }">
          <!-- <el-button type="primary" icon="el-icon-edit" circle></el-button> -->
          <el-tooltip content="修改" placement="top" slot="reference">
            <el-button type="primary" icon="el-icon-edit" @click="ubpateBannar(row)"></el-button>
          </el-tooltip>
          <el-popconfirm title="是否删除？" icon="el-icon-info" icon-color="red" @confirm="getDelete(row.id,row.sortId)">
            <el-tooltip content="删除" placement="top" slot="reference">
              <el-button type="danger" icon="el-icon-delete"></el-button>
            </el-tooltip>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 修改对话框  -->
    <el-dialog title="修改" :visible.sync="dialogFormVisible">
      <el-form style="width:80%" :model="tmFrom" :rules="rules" ref="ruleForm">
        <!--        <el-form-item label="会员等级" label-width="100px" prop="memberLevel">
          <div>{{ tmFrom.memberLevel == 1 ? "vip" : "Svip" }}</div>
        </el-form-item> -->
        <el-form-item label="问题内容" label-width="100px" prop="questionContent">
          <el-input autocomplete="off" v-model="tmFrom.questionContent"></el-input>
        </el-form-item>
        <el-form-item label="回答内容" label-width="100px" prop="answerContent">
          <el-input autocomplete="off" v-model="tmFrom.answerContent"></el-input>
        </el-form-item>
        <!-- <el-form-item label="消费金额" label-width="100px" prop="costMoney">
          <el-input autocomplete="off" v-model="tmFrom.costMoney"></el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="getAddBannerImage" v-if="state">确 定</el-button>
        <el-button type="primary" @click="UpdateBillingRules" v-else>修改</el-button>
      </div>
    </el-dialog>

    <!-- 添加对话框 -->
    <el-dialog title="添加" :visible.sync="dialogFormVisible1">
      <el-form style="width:80%" :model="list1" :rules="rules" ref="ruleForm">
        <el-form-item label="问题类型" label-width="100px">
         <el-select v-model="list1.sortId">
           <el-option v-for="v in sortList" :key="v.id" :value="v.id" :label="v.sortName">
           </el-option>
         </el-select>
         </el-form-item>
<!--        <el-form-item label="是否自定义">
          <el-select v-model="list1.memberLevel" placeholder="请选择是否自定义" prop="memberLevel">
            <el-option label="vip" value="1"></el-option>
            <el-option label="svip" value="2"></el-option>
          </el-select>
        </el-form-item>
 --><!--
        <el-form-item label="会员类型">
          <el-select v-model="list1.memberLevelName" placeholder="请选择是否自定义" prop="memberLevelName">
            <el-option label="月卡" value="月卡"></el-option>
            <el-option label="季卡" value="季卡"></el-option>
            <el-option label="年卡" value="年卡"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="问题内容" label-width="100px" prop="questionContent">
          <el-input autocomplete="off" v-model="list1.questionContent"></el-input>
        </el-form-item>
        <el-form-item label="回答内容" label-width="100px" prop="answerContent">
          <el-input autocomplete="off" v-model="list1.answerContent"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="addQuestionAnswer">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'questionList',
    data() {

      return {
        //列表数据
        list: [],
        sortList: [],
        sName: '',
        rName:'',
        sortId:'',
        list1: {
          // costMoney: "",
          // memberLevel: "",
          // memberLevelName: "",
          // monthNum: ""
        },
        // 对话框显示与隐藏控制
        dialogFormVisible: false,
        dialogFormVisible1: false,
        // 表单对象
        tmFrom: {},
        // 当前为修改还是添加 修改为false 添加为true
        state: true,
        // 表单验证
        rules: {
          name: [{
            required: true,
            message: '请输入规则名称',
            trigger: 'blur'
          }],
          proportion: [{
              required: true,
              message: '请选择计费比列，数字和小数',
              trigger: 'blur'
            },
            {
              transform(value) {
                return Number(value);
              },
              validator(rule, value, callback) {
                if (Number.isFinite(value) && value > 0) {
                  callback();
                } else {
                  callback(new Error("请输入类型为数字和小数，并不少于0"));
                }
              },
              trigger: "blur",
            }
          ],
          intro: [{
            required: true,
            message: '请选择规则简介',
            trigger: 'blur'
          }]
        }

      }
    },
    mounted() {
      this.getQuestionSortList()
    },

    methods: {
      handleCommand(command) {
        // console.log(command)
        this.sName = this.sortList[command].sortName
        this.getQuestionAnswerList(this.sortList[command].id)
        this.sortId = this.sortList[command].id
      },
      handleCommand2(command) {
        console.log(command)
         this.getHotQuestionSortList()
        // this.sName = this.sortList[command].sortName
        // this.getQuestionAnswerList(this.sortList[command].id)
      },
      // 获取热门问题列表
      async getHotQuestionSortList() {
        let res = await this.$API.question.getHotQuestionSortList()
        console.log(res, "热门问题列表")
        // this.sortList = res.data;
        // this.getQuestionAnswerList(this.sortList[0].id)
      },
      // 获取问题种类列表
      async getQuestionSortList() {
        let res = await this.$API.question.getQuestionSortList()
        console.log(res, "问题种类列表")
        this.sortList = res.data;
        this.getQuestionAnswerList(this.sortId)
      },
      // 获取问题回答列表
      async getQuestionAnswerList(sortId) {
        let res = await this.$API.question.getQuestionAnswerList({
          sortId,
        })
        console.log(res, "问题回答列表")
        this.list = res.data;

      },

      // 取消
      getShow() {
        this.state = true
        this.dialogFormVisible = true
        this.tmFrom = {
          name: '',
          proportion: '',
          intro: ''
        }
      },

      add() {
        this.dialogFormVisible1 = true
      },
      // 刷新
      getDynamicExtensionProcessList() {
        this.getQuestionAnswerList(this.sortId)
      },
      // 添加
      getAddBannerImage() {
        // console.log(this.getMemberLevelConfigurationList());
      },
      // 添加一条会员套餐
      addQuestionAnswer() {
        // console.log(this.list1)
        //    return false;
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            let res = await this.$API.question.addQuestionAnswer(this.list1);
            this.dialogFormVisible1 = false;
           this.getQuestionAnswerList(this.sortId)
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      },

      // 点击修改
      ubpateBannar(row) {
        this.state = false
        this.dialogFormVisible = true;
        // 浅拷贝  让tmFrom收到的数据非服务器直接传回的数据
        this.tmFrom = {
          ...row
        };
      },
      // 点击修改
      UpdateBillingRules() {
        console.log(this.tmFrom)
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            let res = await this.$API.question.updateQuestionAnswer(this.tmFrom);
            this.dialogFormVisible = false;
            this.getQuestionAnswerList(this.tmFrom.id)
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      },
      // 删除
      async getDelete(id, id2) {
        let res = await this.$API.question.deleteQuestionAnswer({
          id: id
        });
        this.getQuestionAnswerList(id2)

      }
    }
  }
</script>

<style lang="scss">
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }

  .el-icon-arrow-down {
    font-size: 12px;
  }
</style>
